package com.stepshop.core.service.impl;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.stepshop.core.dao.BaseMapper;

public abstract class BaseServiceImpl<K,V> {
	//Autowired可以用    Resource 不可以用
//	@Resource(type=BaseAreaMapper.class)
//	V mapper;
	@Autowired
	protected V mapper;
	Class clazzMapper=null;
	Class clazzEntity=null;
	public BaseServiceImpl() {
		ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
		clazzEntity = (Class<K>) type.getActualTypeArguments()[0];
		clazzMapper = (Class<V>) type.getActualTypeArguments()[1];
	}
	@SuppressWarnings("unchecked")
	public K queryById(Long id) {
		K k=null;
		try {
			k = (K)((BaseMapper<K>)mapper).selectByPrimaryKey(id);
//			Method method = clazzMapper.getMethod("selectByPrimaryKey", Long.class);
//			k = (K) method.invoke(clazzMapper.newInstance(),id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return k;
	}
	@SuppressWarnings("unchecked")
	public int deleteById(Long id) {
		K k=null;
		try {
			return ((BaseMapper<K>)mapper).deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	@SuppressWarnings("unchecked")
	public List<K> queryAll() {
		return ((BaseMapper<K>)mapper).selectAll();
	}
	public int updateById(K k) {
		return 0;
	}
	public int add(K k){
		return ((BaseMapper<K>)mapper).insert(k);
	}
}
